#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:models.py
# author:wmf
# datetime:2021/5/31 10:07
# software: PyCharm
'''
this is function description
'''
# import module your need
import json

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
bootstrap = Bootstrap(app)

app.config.from_object('config')
db = SQLAlchemy(app)


class Message(db.Model):
    __tablename__ = 'message'

    autoID = db.Column(db.Integer, primary_key=True)
    title1 = db.Column(db.String(255), info='留言标题')
    title2 = db.Column(db.Text, info='留言标题')
    datetime = db.Column(db.DateTime, server_default=db.FetchedValue(), info='留言时间')
    status = db.Column(db.String(255), info='状态')

    # 单个对象方法1
    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict

    # 单个对象方法2
    def single_to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    # 多个对象
    def dobule_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self, key) is not None:
                result[key] = str(getattr(self, key))
            else:
                result[key] = getattr(self, key)
        return result
